package com.ruoyi.web.web.controller;

import com.ruoyi.web.core.config.AppConf;
import com.ruoyi.accountant.domain.ReasonDetail;
import com.ruoyi.accountant.service.ReasonDetailService;
import com.ruoyi.web.web.form.ReasonDetailForm;
import com.ruoyi.web.web.form.Validation;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/reasonDetail")
public class ReasonDetailController {

    @Autowired
    private ReasonDetailService reasonDetailService;

    @PostMapping("/export")
    public void export(ReasonDetailForm form, HttpServletResponse response) throws IOException {

        Validation validate = form.validate();
        if(validate.getOk()) {

            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;fileName=reason.xls");

            List<ReasonDetail> allByForm = reasonDetailService.findByForm(form);
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet();
            HSSFRow row = sheet.createRow(0);

            row.createCell(0, CellType.STRING).setCellValue("交易类型");
            row.createCell(1, CellType.STRING).setCellValue("原因");
            row.createCell(2, CellType.STRING).setCellValue("日期");
            row.createCell(3, CellType.STRING).setCellValue("附注说明");
            row.createCell(4, CellType.STRING).setCellValue("借方金额");
            row.createCell(5, CellType.STRING).setCellValue("贷方金额");
            row.createCell(6, CellType.STRING).setCellValue("对象");
            row.createCell(7, CellType.STRING).setCellValue("标签");
            row.createCell(8, CellType.STRING).setCellValue("营业税");
            row.createCell(9, CellType.STRING).setCellValue("发票号码");

            int rowNum = 1;
            for (ReasonDetail detail : allByForm) {
                HSSFRow row1 = sheet.createRow(rowNum++);

                row1.createCell(0, CellType.STRING).setCellValue(detail.getDtype());
                row1.createCell(1, CellType.STRING).setCellValue(detail.getOfReasonObject().getRname());
                row1.createCell(2, CellType.STRING).setCellValue(AppConf.DATE_FORMAT.format(detail.getTransactionDate()));

                row1.createCell(3, CellType.STRING).setCellValue(detail.getComment());
                row1.createCell(4, CellType.STRING).setCellValue(detail.getCreditorMoney().toString());
                row1.createCell(5, CellType.STRING).setCellValue(detail.getDebtorMoney().toString());
                row1.createCell(6, CellType.STRING).setCellValue(detail.getObject());
                row1.createCell(7, CellType.STRING).setCellValue(detail.getTag());
                row1.createCell(8, CellType.STRING).setCellValue(detail.getTax().toString());
                row1.createCell(9, CellType.STRING).setCellValue(detail.getInvoice());
            }

            workbook.write(response.getOutputStream());
            workbook.close();
        }
    }
}
